package com.cd.serlet;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;

import com.cd.dao.StudentDao;
import com.cd.entity.Student;
import com.cd.util.PageResult;
import com.cd.vo.StudentVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static jdk.nashorn.internal.objects.NativeString.search;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentDao studentDao;
    @Override
    public void init() throws ServletException {
        studentDao = new StudentDao();
    }
    //增删改查
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("application/json;charset=utf-8");
            req.setCharacterEncoding("utf-8");
//          1.请求参数cmd
            String cmd = req.getParameter("cmd");
            if(StrUtil.isNotBlank(cmd)){//cmd不为空且长度去掉空格后大于0
                if("list".equals(cmd)) {
                    list(req,resp);
                }
                if("findByPage".equals(cmd)){//分页查询
                    findByPage(req,resp);
                }
                if("search".equals(cmd)){//条件查询带分页
                    search(req,resp);
                }
                if("add".equals(cmd)){
                    add(req,resp);
                }
                if("update".equals(cmd)){
                    update(req,resp);
                }
            }
    }
    //添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //封装为java对象
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        //返回结果

        Map<String,String> map = new HashMap<>();
        try {
            // 添加学生
            studentDao.update(student);

            map.put("code","200");
            map.put("message","添加学生成功");
        } catch (IOException e) {
            map.put("code","500");
            map.put("message","添加学生失败");
        }
        resp.getWriter().write(JSONUtil.toJsonStr(map));
        resp.getWriter().flush();
    }

    //修改学生信息
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        studentDao.update(student);
        //返回结果
        Map<String,String> map = new HashMap<>();
        try {
            studentDao.add(student);
           //修改学生

            map.put("code","200");
            map.put("message","修改学生成功");
        } catch (IOException e) {
            map.put("code","500");
            map.put("message","修改学生失败");
        }
        resp.getWriter().write(JSONUtil.toJsonStr(map));
        resp.getWriter().flush();

    }


    //查询学生信息
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        // 得到查询条件
        ObjectMapper objectMapper = new ObjectMapper();
        StudentVo studentVo = objectMapper.readValue(req.getInputStream(), StudentVo.class);

        // 开始分页带条件查询
        PageResult<Student> pageResult = studentDao.search(page,pageSize,studentVo);
        String json = JSONUtil.toJsonStr(pageResult);

        resp.getWriter().write(json);
        resp.getWriter().flush();
    }
    //分页查询
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获取分页参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageResult<Student> pageResult = studentDao.findByPage(page,pageSize);
        String json = JSONUtil.toJsonStr(pageResult);
        resp.getWriter().write(json);
        resp.getWriter().flush();
    }

//列表学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> students = studentDao.findAll();//查询所有学生 信息
        //转换为json字符串并送到前端
        String json = JSONUtil.toJsonStr(students);
        resp.getWriter().write(json);
        resp.getWriter().flush();

    }

}
